home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / sync-ical.c < prev    next >
C/C++ Source or Header  |  1997-08-06  |  20KB  |  844 lines

  1. /* sync-ical.c:  Synchronize ical calendar file
  2.  *
  3.  * Copyright (c) 1996, Kenneth Albanowski
  4.  *
  5.  * This is free software, licensed under the GNU Public License V2.
  6.  * See the file COPYING for details.
  7.  */
  8.  
  9. /* This program is an experiment in synchronizing the Pilot datebook
  10.    database with an Ical database. It will not do anything useful for you.
  11.    Yet. If you are interested in exploring the syncronization process, read
  12.    lib/sync.c and sync-memodir.c */
  13.    
  14. /* At the moment, this file is an absolute mess, but it's functional, more
  15.    or less */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <stdarg.h>
  21. #include "pi-source.h"
  22. #include "pi-socket.h"
  23. #include "pi-sync.h"
  24. #include "pi-datebook.h"
  25. #include "pi-todo.h"
  26. #include "pi-dlp.h"
  27.  
  28. /* Set up a quick'n'dirty emulation of the Tcl interpreter commands */
  29. typedef struct {
  30.     int r, w;
  31.     char * result;
  32. } Tcl_Interp;
  33.  
  34. int Tcl_VarEval(Tcl_Interp * interp, ...) {
  35.     static char result[4096];
  36.     int l;
  37.     va_list args;
  38.     
  39.     va_start(args, interp);
  40.     
  41.     write(interp->w, "puts [catch {", 13);
  42.     for(;;) {
  43.         char * c = va_arg(args, char*);
  44.         if (!c)
  45.             break;
  46.         
  47.         write(interp->w, c, strlen(c));
  48.     }
  49.     write(interp->w, " } retval]\nputs -nonewline $retval\nflush stdout\n", 48);
  50.     
  51.     va_end(args);
  52.     
  53.     l = read(interp->r, result, 4096);
  54.     if (l<0)
  55.         l=0;
  56.     result[l] = '\0';
  57.     
  58.     interp->result = result;
  59.  
  60.     while(*interp->result != '\n')
  61.         interp->result++;
  62.         
  63.     *((interp->result)++) = '\0';
  64.     
  65.     return atoi(result);;
  66. }
  67.  
  68. int Tcl_Eval(Tcl_Interp * interp, char * cmd) {
  69.     return Tcl_VarEval(interp, cmd, NULL);
  70. }
  71.  
  72. char * tclquote(char * in) {
  73.   static char * buffer = 0;
  74.   char * out;
  75.   char * pos;
  76.   int len;
  77.  
  78.   len = 3;
  79.   pos = in;
  80.   while(*pos) {
  81.     if((*pos == '\\') || (*pos == '"') || (*pos == '[') || (*pos == '{') || (*pos == '$'))
  82.       len++;
  83.     len++;
  84.     pos++;
  85.   }
  86.  
  87.   if (buffer)
  88.     free(buffer);
  89.   buffer = (char*)malloc(len);
  90.   out = buffer;
  91.  
  92.   pos = in;
  93.   *out++ = '"';
  94.   while(*pos) {
  95.     if((*pos == '\\') || (*pos == '"') || (*pos == '[') || (*pos == '{') || (*pos == '$'))
  96.       *out++ = '\\';
  97.     *out++=*pos++;
  98.   }
  99.   *out++ = '"';
  100.   *out++ = '\0';
  101.  
  102.   return buffer;
  103. }
  104.  
  105.  
  106.  
  107. /* Define our own LocalRecord type. */
  108. struct LocalRecord {
  109.      StandardLocalRecord;
  110.      char * item; /* non-null and a name if an Ical item */
  111.      unsigned long deleted;  /* non-null if only a deletion ID */
  112. };
  113.                                         
  114. /* Define our own SyncAbs type */
  115. struct SyncAbs {
  116.      StandardSyncAbs;
  117.      /* items down here are equivalent of C++ class data.
  118.      Any information needed to talk to databases, etc., goes in here.
  119.       */
  120.     Tcl_Interp * ical;
  121.     char * cal;
  122.     int todo; /* Are we interested in todos or memos? */
  123. };
  124.  
  125. /* Get a Pilot ID for a local record, or 0 if no Pilot ID has been set. Any
  126.    local ID mechanism is not relevent, only IDs given by the Pilot. */
  127. unsigned long GetPilotID(SyncAbs * thisSA,LocalRecord * Local) {
  128.     char * var;
  129.     /*char id[10];*/
  130.  
  131.     if (Local->deleted)
  132.         return Local->deleted;
  133.         
  134.     Tcl_VarEval(thisSA->ical, 
  135.       "set result 0\n",
  136.       "catch { set result [", Local->item, " option sync_id]}\n",
  137.       "set result",
  138.       NULL);
  139.  
  140.         if ((thisSA->ical->result[0] - '0') != thisSA->todo)
  141.           return 0;
  142.     
  143.     var = thisSA->ical->result+1;
  144.     
  145.     return atoi(var);
  146. }
  147.  
  148. /* Set the ID on a local record to match a given Pilot ID. */
  149. int SetPilotID(SyncAbs * thisSA,LocalRecord * Local, unsigned long ID) {
  150.     char buf[40];
  151.     sprintf(buf,"%d%lu", thisSA->todo?1:0, ID);
  152.     Tcl_VarEval(thisSA->ical, Local->item, " option sync_id ",buf,NULL);
  153.     return 1;
  154. }
  155.  
  156. /* Given a PilotRecord, try and find a local record with a matching ID */
  157. int MatchRecord(SyncAbs * thisSA,  LocalRecord ** Local, PilotRecord * p) {
  158.     static LocalRecord lr;
  159.     char buf[40];
  160.     sprintf(buf,"%d%lu", thisSA->todo?1:0, p->ID);
  161.     
  162.     /* Check active items */
  163.     Tcl_VarEval(thisSA->ical,
  164.       "set result \"\"\n", 
  165.       thisSA->cal, " incalendar [", thisSA->cal, " main] item {\n",
  166.       "  catch { \n",
  167.       "    if {[string compare [$item option sync_id] \"", buf, "\"]==0} {\n",
  168.       "      set result $item\n",
  169.       "    }\n",
  170.       "  }\n",
  171.       "}\n",
  172.       "set result",
  173.           NULL);
  174.     
  175.     if (strlen(thisSA->ical->result)) {
  176.         *Local = &lr;
  177.         lr.item = strdup(thisSA->ical->result);
  178.         lr.deleted = 0;
  179.         return 1;
  180.     }
  181.     
  182.     /* Check deleted items */
  183.     Tcl_VarEval(thisSA->ical,
  184.           "set result \"\"\n",
  185.           "catch {\n",
  186.           "  set deleted [",thisSA->cal," option deleted]\n",
  187.           "  foreach item $deleted {\n",
  188.           "    if {[string compare $item  \"",buf,"\"]==0} {\n",
  189.           "      set result $item\n",
  190.           "      break\n",
  191.           "    }\n",
  192.           "  }\n",
  193.           "}\n",
  194.           "set result",
  195.           NULL);
  196.           
  197.     if (strlen(thisSA->ical->result)) {
  198.         *Local = &lr;
  199.         lr.item = 0;
  200.         lr.deleted = atoi(thisSA->ical->result+1);
  201.         return 1;
  202.     } else {
  203.         *Local = 0;
  204.         return 0;
  205.     }
  206. }
  207.  
  208. /* Free up the LocalRecord returned by MatchRecord */
  209. int FreeMatch(SyncAbs * thisSA,LocalRecord ** Local) {
  210.     *Local = 0;
  211.     return 0;
  212. }
  213.  
  214.  
  215. /* Iterate over all LocalRecords, in arbitrary order */
  216. int Iterate(SyncAbs * thisSA, LocalRecord ** Local) {
  217.     static LocalRecord lr;
  218.     
  219.     if (!*Local) {
  220.       Tcl_VarEval(thisSA->ical,
  221.             "set active {}\n",
  222.             thisSA->cal," incalendar [", thisSA->cal, " main] item {\n",
  223.             "  # some dohicky to make sure record's todo matches thisSA->todo\n",
  224.             "  lappend active $item\n",
  225.             "}\n",
  226.             "set deleted {}\n",
  227.             "catch {\n",
  228.             "  set deleted [",thisSA->cal," option deleted]\n",
  229.             "}\n",
  230.             NULL);
  231.     }
  232.     
  233.     Tcl_VarEval(thisSA->ical,
  234.       "set result {}\n",
  235.       "if {[llength $active]>0} {\n",
  236.       "  set result [lindex $active 0]\n",
  237.       "  set active [lreplace 1 end]\n",
  238.       "}\n",
  239.       "set result",
  240.       NULL);
  241.     
  242.     if (strlen(thisSA->ical->result)) {
  243.         *Local = &lr;
  244.         lr.item = strdup(thisSA->ical->result);
  245.         lr.deleted = 0;
  246.         return 1;
  247.     }
  248.  
  249.     Tcl_VarEval(thisSA->ical,
  250.       "set result {}\n",
  251.       "if {[llength $deleted]>0} {\n",
  252.       "  set result [lindex $deleted 0]\n",
  253.       "  set deleted [lreplace $deleted 1 end]\n",
  254.       "}\n",
  255.       "set result",
  256.       NULL);
  257.  
  258.     
  259.     if (strlen(thisSA->ical->result)) {
  260.         *Local = &lr;
  261.         lr.item = 0;
  262.         lr.deleted = atoi(thisSA->ical->result);
  263.         return 1;
  264.     }
  265.  
  266.     return *Local!=0;
  267. }
  268.  
  269. /* Iterate over local records of a specified type. */
  270. int IterateSpecific(SyncAbs * thisSA, LocalRecord ** Local, int flag, int archived)
  271. {
  272.     static LocalRecord lr;
  273.     
  274.     if (!*Local) {
  275.       Tcl_VarEval(thisSA->ical,
  276.         "set active {}\n",
  277.         thisSA->cal," incalendar [", thisSA->cal, " main] item {\n",
  278.         "  lappend active $item\n",
  279.         "}\n",
  280.         "set deleted {}\n",
  281.         "catch {\n",
  282.         "  set deleted [",thisSA->cal," option deleted]\n",
  283.         "}\n",
  284.         NULL);
  285.     }
  286.     
  287.     Tcl_VarEval(thisSA->ical,
  288.       "set result {}\n",
  289.       "if {[llength $active]>0} {\n",
  290.       "  set result [lindex $active 0]\n",
  291.       "  set active [lreplace 1 end]\n",
  292.       "}\n",
  293.       "set result",
  294.       NULL);
  295.     
  296.     if (strlen(thisSA->ical->result)) {
  297.         *Local = &lr;
  298.         lr.item = strdup(thisSA->ical->result);
  299.         lr.deleted = 0;
  300.         return 1;
  301.     }
  302.  
  303.     Tcl_VarEval(thisSA->ical,
  304.       "set result {}\n",
  305.       "if {[llength $deleted]>0} {\n",
  306.       "  set result [lindex $deleted 0]\n",
  307.       "  set deleted [lreplace $deleted 1 end]\n",
  308.       "}\n",
  309.       "set result",
  310.       NULL);
  311.  
  312.     
  313.     if (strlen(thisSA->ical->result)) {
  314.         *Local = &lr;
  315.         lr.item = 0;
  316.         lr.deleted = atoi(thisSA->ical->result);
  317.         return 1;
  318.     }
  319.  
  320.     return *Local!=0;
  321. }
  322.  
  323. /* Given a PilotRecord, store it in the local database */
  324. int StoreRemote(SyncAbs * thisSA,PilotRecord* p) {
  325.     struct Appointment a;
  326.     int j;
  327.     char buf[256];
  328.     
  329.     unpack_Appointment(&a, p->record, p->length);
  330.     
  331.     if (a.event) {
  332.       Tcl_Eval(thisSA->ical,"set i [notice]");
  333.  
  334.     } else {
  335.       int start,end;
  336.  
  337.       Tcl_Eval(thisSA->ical,"set i [appointment]");
  338.       
  339.       start = a.begin.tm_hour*60 + a.begin.tm_min;
  340.       end   = a.end.tm_hour*60 + a.end.tm_min;
  341.  
  342.       sprintf(buf, "%d", start);
  343.       Tcl_VarEval(thisSA->ical,"$i starttime ",buf, NULL);
  344.       sprintf(buf, "%d", end-start+1);
  345.       Tcl_VarEval(thisSA->ical,"$i length ",buf, NULL);
  346.     }
  347.     sprintf(buf, "%lu", p->ID);
  348.         Tcl_VarEval(thisSA->ical,"$i option sync_id ",buf, NULL);
  349.     
  350.         Tcl_VarEval(thisSA->ical,"$i text ",tclquote(a.description), NULL);
  351.     
  352.     sprintf(buf,"set begin [date make %d %d %d]\n", a.begin.tm_mday,a.begin.tm_mon+1,a.begin.tm_year+1900);
  353.     Tcl_Eval(thisSA->ical,buf);
  354.     
  355.     if (a.repeatFrequency) {
  356.       if (a.repeatType == repeatDaily) {
  357.         sprintf(buf,"$i dayrepeat %d $begin", a.repeatFrequency);
  358.         Tcl_Eval(thisSA->ical,buf);
  359.       } else if(a.repeatType == repeatMonthlyByDate) {
  360.         sprintf(buf,"$i month_day %d $begin %d", a.begin.tm_mon+1,a.repeatFrequency);
  361.         Tcl_Eval(thisSA->ical,buf);
  362.       } else if(a.repeatType == repeatMonthlyByDay) {
  363.         if (a.repeatDay>=domLastSun) {
  364.           sprintf(buf,"$i month_last_week_day %d 1 $begin %d\n", a.repeatDay % 7 + 1,
  365.                                                         a.repeatFrequency);
  366.           Tcl_Eval(thisSA->ical,buf);
  367.         } else {
  368.           sprintf(buf,"$i month_week_day %d %d $begin %d\n", a.repeatDay % 7 + 1,
  369.                                                         a.repeatDay / 7 + 1,
  370.                                                         a.repeatFrequency);
  371.           Tcl_Eval(thisSA->ical,buf);
  372.         }
  373.       } else if(a.repeatType == repeatWeekly) {
  374.         int i;
  375.         strcpy(buf,"$i weekdays ");
  376.         for (i=0;i<7;i++)
  377.           if (a.repeatDays[i])
  378.             sprintf(buf+strlen(buf), "%d ", i+1);
  379.             strcat(buf,"\n");
  380.             Tcl_Eval(thisSA->ical,buf);
  381.       } else if(a.repeatType == repeatYearly) {
  382.         sprintf(buf,"$i monthrepeat %d $begin\n", 12 * a.repeatFrequency);
  383.         Tcl_Eval(thisSA->ical,buf);
  384.       }
  385.       Tcl_Eval(thisSA->ical,"$i start $begin\n");
  386.       if (!a.repeatForever) {
  387.         sprintf(buf,"$i finish [date make %d %d %d]\n", a.repeatEnd.tm_mday, a.repeatEnd.tm_mon+1, 
  388.                                                    a.repeatEnd.tm_year+1900);
  389.         Tcl_Eval(thisSA->ical,buf);
  390.       }
  391.       if (a.exceptions)
  392.         for (j=0;j<a.exceptions;j++) {
  393.           sprintf(buf,"$i deleteon [date make %d %d %d]\n", a.exception[j].tm_mday,
  394.                                                        a.exception[j].tm_mon+1,
  395.                                                        a.exception[j].tm_year+1900);
  396.           Tcl_Eval(thisSA->ical,buf);
  397.         }
  398.     } else 
  399.           Tcl_Eval(thisSA->ical,"$i date $begin\n");
  400.     
  401.     Tcl_VarEval(thisSA->ical,thisSA->cal," add $i\n",NULL);
  402.  
  403.     free_Appointment(&a);
  404.  
  405.     return 0;    
  406. }
  407.  
  408.  
  409. /* Delete all local records */
  410. int DeleteAll(SyncAbs * thisSA) {
  411.  
  412.     Tcl_VarEval(thisSA->ical,
  413.       thisSA->cal," incalendar [", thisSA->cal, " main] item {\n",
  414.       "  $item delete\n",
  415.       "}\n",
  416.       thisSA->cal," option deleted {}",
  417.       NULL);
  418.     return 0;
  419. }
  420.  
  421.  
  422.  
  423. /*===========================================================*/
  424. #if 0
  425. /*  These are the important functions below. They implement the interface that
  426.     the abstract synchronization layer invokes */
  427.  
  428. /* Get a Pilot ID for a local record, or 0 if no Pilot ID has been set. Any
  429.    local ID mechanism is not relevent, only IDs given by the Pilot. */
  430. unsigned long GetPilotID(SyncAbs * thisSA,LocalRecord * Local) {
  431.     return Local->ID;
  432.     return 1;
  433. }
  434.  
  435. /* Set the ID on a local record to match a given Pilot ID. */
  436. int SetPilotID(SyncAbs * thisSA,LocalRecord * Local, unsigned long ID) {
  437.     Local->ID = ID;
  438.     return 1;
  439. }
  440.  
  441. /* Given a PilotRecord, try and find a local record with a matching ID */
  442. int MatchRecord(SyncAbs * thisSA,  LocalRecord ** Local, PilotRecord * p) {
  443.     LocalRecord * m = memos;
  444.     while(m) {
  445.         if(m->ID == p->ID)
  446.             break;
  447.         m=m->next;
  448.     }
  449.     if(m)
  450.         *Local = m;
  451.     else
  452.         *Local = 0;
  453.     
  454.     return 1;
  455. }
  456.  
  457. /* Free up the LocalRecord returned by MatchRecord */
  458. int FreeMatch(SyncAbs * thisSA,LocalRecord ** Local) {
  459.     *Local = 0;
  460.     return 0;
  461. }
  462.  
  463. /* Iterate over all LocalRecords, in arbitrary order */
  464. int Iterate(SyncAbs * thisSA, LocalRecord ** Local) {
  465.     LocalRecord * m = *Local;
  466.     if( !m) {
  467.         m = memos;
  468.     } else {
  469.         m=m->next;
  470.     }
  471.     *Local = m;
  472.     return m!=0;
  473. }
  474.  
  475. /* Iterate over local records of a specified type. */
  476. int IterateSpecific(SyncAbs * thisSA, LocalRecord ** Local, int flag, int archived) {
  477.     LocalRecord * m = *Local;
  478.     if( !m) {
  479.         m = memos;
  480.     } else {
  481.         m=m->next;
  482.     }
  483.     while(m) {
  484.             if(archived) {
  485.               if(m->archived)
  486.                 break;
  487.             } else {
  488.           if(m->attr == flag)
  489.             break;
  490.         }
  491.         m=m->next;
  492.     }
  493.     *Local = m;
  494.     return m!=0;
  495. }
  496.  
  497. /* Set status of local record */
  498. int SetStatus(SyncAbs * thisSA,LocalRecord * Local, int status) {
  499.     Local->attr = status;
  500.     return 0;
  501. }
  502.  
  503. /* There is no GetStatus, the abstract layer uses Local->attr */
  504.  
  505. /* Set archival status of local record */
  506. int SetArchived(SyncAbs * thisSA,LocalRecord * Local,int archived) {
  507.         Local->archived = archived;
  508.     return 0;
  509. }
  510.  
  511. /* There is no GetStatus, the abstract layer uses Local->archived */
  512.  
  513. /* Given a PilotRecord, store it in the local database */
  514. int StoreRemote(SyncAbs * thisSA,PilotRecord* p) {
  515.     LocalRecord * m;
  516.     int h;
  517.     struct stat stbuf;
  518.     
  519.     if(p->ID != 0) {
  520.         /* replace record */
  521.         m = memos;
  522.         while(m) {
  523.             if(m->ID == p->ID)
  524.                 break;
  525.             m=m->next;
  526.         }
  527.         if (m) {
  528.             h = open(filename(m->name),O_WRONLY|O_CREAT|O_TRUNC,0666);
  529.             write(h, p->record, p->length-1);
  530.             write(h, "\n", 1);
  531.             close(h);
  532.             stat(filename(m->name),&stbuf);
  533.             m->mtime = stbuf.st_mtime;
  534.             return 0;
  535.         }
  536.     }
  537.     /* new record */
  538.     m = (LocalRecord*)malloc(sizeof(LocalRecord));
  539.     m->ID = p->ID;
  540.     m->attr = p->attr;
  541.     m->secret = p->secret;
  542.     
  543.     strcpy(m->name, newfilename(p));
  544.     
  545.     m->next = memos;
  546.     memos = m;
  547.  
  548.     h = open(filename(m->name),O_WRONLY|O_CREAT|O_TRUNC,0666);
  549.     write(h, p->record, p->length-1);
  550.     write(h, "\n", 1);
  551.     close(h);
  552.  
  553.     stat(filename(m->name),&stbuf);
  554.     m->mtime = stbuf.st_mtime;
  555.     
  556.     return 0;    
  557. }
  558.  
  559. /* Given a local record, construct a PilotRecord suitable for transmission
  560. to a Pilot */
  561. PilotRecord * Transmit(SyncAbs* thisSA ,LocalRecord* Local) {
  562.         static PilotRecord p;
  563.     int h = open(filename(Local->name),O_RDONLY);
  564.     
  565.     struct stat statbuf;
  566.     stat(filename(Local->name),&statbuf);
  567.     p.length = statbuf.st_size+1;
  568.     p.record = (unsigned char*)malloc(p.length);
  569.     read(h, p.record, p.length-1);
  570.     p.record[p.length-1] = '\0';
  571.     close(h);
  572.     
  573.     p.category = 0;
  574.     p.attr = Local->attr;
  575.     p.archived = Local->archived;
  576.     p.secret = Local->secret;
  577.     
  578.     return &p;
  579. }
  580.  
  581. /* Free PilotRecord created by Transmit */
  582. int FreeTransmit(SyncAbs* thisSA,LocalRecord* Local,PilotRecord* Remote) {
  583.     free(Remote->record);
  584.     return 0;
  585. }
  586.  
  587. /* Find a local backup record and compare it to the pilot record for inequality */
  588. int CompareBackup(SyncAbs * thisSA, LocalRecord* m, PilotRecord* p) {
  589.     char buffer[0xffff];
  590.     int r,len;
  591.  
  592.       r = open(backupname(m->name),O_RDONLY);
  593.       
  594.       if (r<0)
  595.         return -1; /* "less", arbitrary */
  596.       
  597.       len = read(r,buffer,0xffff);
  598.     close(r);
  599.     
  600.     if(len != p->length)
  601.         return -1; /* "less", arbitrary */
  602.  
  603.     if(buffer[len-1] == '\n')
  604.         buffer[len-1] = '\0';
  605.     
  606.     return memcmp(buffer, p->record, len);
  607. }
  608.  
  609. /* Compare a local record and pilot record for inequality */
  610. int Compare(SyncAbs * thisSA, LocalRecord * m, PilotRecord* p) {
  611.     char buffer[0xffff];
  612.       int r = open(filename(m->name),O_RDONLY);
  613.       int len = read(r,buffer,0xffff);
  614.     close(r);
  615.  
  616.     if(len != p->length)
  617.         return -1; /* "less", arbitrary */
  618.  
  619.     if(buffer[len-1] == '\n')
  620.         buffer[len-1] = '\0';
  621.     
  622.     return memcmp(buffer, p->record, len);
  623. }
  624.  
  625. /* Delete all local records */
  626. int DeleteAll(SyncAbs * thisSA) {
  627.     while(memos) {
  628.         LocalRecord * m = memos;
  629.         memos = memos->next;
  630.         if(m->attr != RecordDeleted) {
  631.             unlink(filename(m->name));
  632.         }
  633.         free(m);
  634.     }
  635.     return 0;
  636. }
  637.  
  638. /* Do a local purge, deleting all records marked Deleted, and
  639.    archiving all records marked for archiving */
  640. int Purge(SyncAbs * thisSA) {
  641.     LocalRecord * prev = 0;
  642.     LocalRecord * m = memos, *next;
  643.     while(m) {
  644.         next = m->next;
  645.         if((m->attr == RecordDeleted) || (m->archived)) {
  646.             unlink(filename(m->name));
  647.             if(prev)
  648.                 prev->next = next;
  649.             else
  650.                 memos=next;
  651.             free(m);
  652.         } else
  653.             prev = m;
  654.         m = next;
  655.     }
  656.     return 0;
  657. }
  658.  
  659. /* Add remote record to archive. l is non-NULL if there is a matching local record */
  660. int ArchiveRemote(SyncAbs * s, LocalRecord * l, PilotRecord * p) {
  661.     char name[256];
  662.     int h;
  663.     strcpy(name,"Memos/Archive/memoXXXXXX");
  664.  
  665.     h = open(newarchivename(p,l),O_WRONLY|O_CREAT|O_TRUNC,0666);
  666.     write(h, p->record, p->length-1);
  667.     write(h, "\n", 1);
  668.     close(h);
  669.     
  670.     return 0;
  671. }
  672.  
  673. #endif
  674. /*===========================================================*/
  675.                                                                                          
  676.                                                                                          
  677. /*typedef struct{
  678.     char calname[40];
  679.     int r,w;
  680. } LocalData;
  681.  
  682. typedef struct {
  683.     char recname[40];
  684.     long PilotID;
  685. } LocalRecord;*/
  686.  
  687. void dosend(char*text, int w) {
  688.     write(w,text,strlen(text));
  689.     write(w,"flush stdout\n",13);
  690. }
  691.  
  692. void doread(char*buffer, int r) {
  693.     int l = read(r,buffer,4096);
  694.     buffer[l] = 0;
  695. }
  696.  
  697. int main(int argc, char *argv[])
  698. {
  699.   struct pi_sockaddr addr;
  700.   int db;
  701.   int sd;
  702.   struct PilotUser U;
  703.   int in[2], out[2];
  704.   int ret;
  705.   struct SyncAbs abs;
  706.   /*struct SyncAbs abs;*/
  707. /*   = {MatchRecord, IsNew, IsModified, IsArchived, IsNothing,
  708.                         AppendLocal, AppendArchive, StartIterator, NextIterator};*/
  709.   /*LocalData ld;
  710.   LocalRecord *lrp;*/
  711.   Tcl_Interp ical;
  712.  
  713. #if 0
  714.   /* Set up abstraction structure */  
  715.   abs.MatchRecord = MatchRecord;
  716.   abs.Iterate = Iterate;
  717.   abs.IterateSpecific = IterateSpecific;
  718.   abs.SetStatus = SetStatus;
  719.   abs.SetArchived = SetArchived;
  720.   abs.SetPilotID = SetPilotID;
  721.   abs.GetPilotID = GetPilotID;
  722.   abs.StoreRemote = StoreRemote;
  723.   abs.ArchiveLocal = 0;             /* missing */
  724.   abs.ClearStatusArchiveLocal = 0;  /* likewise */
  725.   abs.ArchiveRemote = ArchiveRemote;
  726.   abs.DeleteAll = DeleteAll;
  727.   abs.Purge = Purge;
  728.   abs.CompareBackup = CompareBackup;
  729.   abs.Compare = Compare;
  730.   abs.Transmit = Transmit;
  731.   abs.FreeTransmit = FreeTransmit;
  732. #endif
  733.   abs.DeleteAll = DeleteAll;
  734.   abs.StoreRemote = StoreRemote;
  735.  
  736. #if 0  
  737.   abs.MatchRecord = MatchRecord;
  738.   abs.IsNew = IsNew;
  739.   abs.GetPilotID = GetPilotID;
  740.   abs.IsModified = IsModified;
  741.   abs.IsArchived = IsArchived;
  742.   abs.IsNothing = IsNothing;
  743.   abs.AppendLocal = AppendLocal;
  744.   abs.AppendArchive = AppendArchive;
  745. #endif  
  746.  
  747.   fprintf(stderr, "A Warning be upon Ye: Here Be Dragons!\nThis program is incomplete, ill-considered, and unreliable!\nDo not Rely on without Consideration of Completing the program...\n\n");
  748.  
  749.   if (argc < 3) {
  750.     fprintf(stderr,"usage:%s %s calfile # Calfile will be overwritten!\n",argv[0],TTYPrompt);
  751.     exit(2);
  752.   }
  753.   if (!(sd = pi_socket(PI_AF_SLP, PI_SOCK_STREAM, PI_PF_PADP))) {
  754.     perror("pi_socket");
  755.     exit(1);
  756.   }
  757.  
  758.   pipe(&in[0]);
  759.   pipe(&out[0]);
  760.   
  761.   if(!fork()) {
  762.       close(in[1]);
  763.       close(out[0]);
  764.       dup2(in[0],fileno(stdin));
  765.       dup2(out[1],fileno(stdout));
  766.       system("ical -f -");
  767.       exit(0);
  768.   }
  769.   
  770.   ical.w = in[1];
  771.   ical.r = out[0];
  772.   
  773.   /*ical = Tcl_CreateInterp();
  774.   
  775.   app_init(ical);*/
  776.   
  777.   Tcl_VarEval(&ical, "calendar calmain ",argv[2]);
  778.  
  779.   abs.ical = &ical;
  780.   abs.cal = "calmain";
  781.   
  782.   addr.pi_family = PI_AF_SLP;
  783.   strcpy(addr.pi_device,argv[1]);
  784.   
  785.   ret = pi_bind(sd, (struct sockaddr*)&addr, sizeof(addr));
  786.   if(ret == -1) {
  787.     perror("pi_bind");
  788.     exit(1);
  789.   }
  790.  
  791.   ret = pi_listen(sd,1);
  792.   if(ret == -1) {
  793.     perror("pi_listen");
  794.     exit(1);
  795.   }
  796.  
  797.   sd = pi_accept(sd, 0, 0);
  798.   if(sd == -1) {
  799.     perror("pi_accept");
  800.     exit(1);
  801.   }
  802.  
  803.   /* Ask the pilot who it is. */
  804.   dlp_ReadUserInfo(sd,&U);
  805.   
  806.   /* Tell user (via Pilot) that we are starting things up */
  807.   dlp_OpenConduit(sd);
  808.   
  809.   /* Open the Datebook's database, store access handle in db */
  810.   if(dlp_OpenDB(sd, 0, dlpOpenReadWrite, "DatebookDB", &db) < 0) {
  811.     puts("Unable to open DatebookDB");
  812.     dlp_AddSyncLogEntry(sd, "Unable to open DatebookDB.\n");
  813.     pi_close(sd);
  814.     exit(1);
  815.   }
  816.   
  817.   CopyFromRemote(sd, db, &abs);
  818.   
  819. /*  SyncDB(sd, &db, &abs, &ld);*/
  820.  
  821.   /* Close the database */
  822.   dlp_CloseDB(sd, db);
  823.   
  824.   dlp_ReadUserInfo(sd, &U);
  825.   
  826.   U.lastSyncPC = 0xDEADBEEF;
  827.     
  828.   dlp_WriteUserInfo(sd, &U);
  829.         
  830.  
  831.   dlp_AddSyncLogEntry(sd, "Read datebook from Pilot.\n");
  832.   
  833.   Tcl_VarEval(&ical,abs.cal," save [",abs.cal," main]",NULL);
  834.  
  835.  
  836.   close(ical.w);
  837.   close(ical.r);
  838.  
  839.   pi_close(sd);  
  840.   
  841.   return 0;
  842. }
  843.  
  844.